home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / g++-dist / cplus-cvt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-25  |  52.2 KB  |  1,768 lines

  1. /* Language-level data type conversion for GNU C++.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.    Hacked by Michael Tiemann (tiemann@mcc.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file contains the functions for converting C expressions
  23.    to different data types.  The only entry point is `convert'.
  24.    Every language front end must have a `convert' function
  25.    but what kind of conversions it does will depend on the language.  */
  26.  
  27. #include "config.h"
  28. #include "tree.h"
  29. #include "cplus-tree.h"
  30. #include "assert.h"
  31.  
  32. #define NULL 0
  33.  
  34. static tree build_up_reference ();
  35.  
  36. /* Change of width--truncation and extension of integers or reals--
  37.    is represented with NOP_EXPR.  Proper functioning of many things
  38.    assumes that no other conversions can be NOP_EXPRs.
  39.  
  40.    Conversion between integer and pointer is represented with CONVERT_EXPR.
  41.    Converting integer to real uses FLOAT_EXPR
  42.    and real to integer uses FIX_TRUNC_EXPR.
  43.  
  44.    Here is a list of all the functions that assume that widening and
  45.    narrowing is always done with a NOP_EXPR:
  46.      In c-convert.c, convert_to_integer.
  47.      In c-typeck.c, build_binary_op_nodefault (boolean ops),
  48.         and truthvalue_conversion.
  49.      In expr.c: expand_expr, for operands of a MULT_EXPR.
  50.      In fold-const.c: fold.
  51.      In tree.c: get_narrower and get_unwidened.
  52.  
  53.    C++: in multiple-inheritance, converting between pointers may involve
  54.    adjusting them by a delta stored within the class definition.  */
  55.  
  56. /* Subroutines of `convert'.  */
  57.  
  58. static tree
  59. convert_to_pointer (type, expr)
  60.      tree type, expr;
  61. {
  62.   register tree intype = TREE_TYPE (expr);
  63.   register enum tree_code form = TREE_CODE (intype);
  64.   
  65.   if (integer_zerop (expr))
  66.     {
  67.       if (type == TREE_TYPE (null_pointer_node))
  68.     return null_pointer_node;
  69.       expr = build_int_2 (0, 0);
  70.       TREE_TYPE (expr) = type;
  71.       return expr;
  72.     }
  73.  
  74.   if (form == POINTER_TYPE)
  75.     {
  76.       intype = TYPE_MAIN_VARIANT (intype);
  77.  
  78.       if (TYPE_MAIN_VARIANT (type) != intype
  79.       && IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype)))
  80.     {
  81.       enum tree_code code = PLUS_EXPR;
  82.       tree basetype = get_base_type (TREE_TYPE (TYPE_MAIN_VARIANT (type)),
  83.                      TREE_TYPE (intype), 1);
  84.       if (basetype == error_mark_node)
  85.         return error_mark_node;
  86.       if (basetype == NULL_TREE)
  87.         {
  88.           basetype = get_base_type (TREE_TYPE (intype),
  89.                     TREE_TYPE (TYPE_MAIN_VARIANT (type)), 1);
  90.           if (basetype == error_mark_node)
  91.         return error_mark_node;
  92.           code = MINUS_EXPR;
  93.         }
  94.       if (basetype)
  95.         {
  96.           if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
  97.           || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
  98.           || DECL_OFFSET (TYPE_NAME (basetype)) != 0)
  99.         {
  100.           /* Need to get the path we took.  */
  101.           tree path;
  102.  
  103.           if (code == PLUS_EXPR)
  104.             get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
  105.           else
  106.             get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
  107.           return build_vbase_path (code, type, expr, path, 0);
  108.         }
  109.         }
  110.     }
  111.       return build1 (NOP_EXPR, type, expr);
  112.     }
  113.  
  114.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  115.     {
  116.       if (type_precision (intype) == POINTER_SIZE)
  117.     return build1 (CONVERT_EXPR, type, expr);
  118.       return convert_to_pointer (type,
  119.                  convert (type_for_size (POINTER_SIZE, 0),
  120.                       expr));
  121.     }
  122.  
  123.   assert (form != OFFSET_TYPE);
  124.  
  125.   if (IS_AGGR_TYPE (intype))
  126.     {
  127.       /* If we cannot convert to the specific pointer type,
  128.      try to convert to the type `void *'.  */
  129.       tree rval;
  130.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  131.       if (rval)
  132.     {
  133.       if (rval == error_mark_node)
  134.         error ("ambiguous pointer conversion");
  135.       return rval;
  136.     }
  137.     }
  138.  
  139.   error ("cannot convert to a pointer type");
  140.  
  141.   return null_pointer_node;
  142. }
  143.  
  144. /* Like convert, except permit conversions to take place which
  145.    are not normally allowed due to visibility restrictions
  146.    (such as conversion from sub-type to private super-type).  */
  147. static tree
  148. convert_to_pointer_force (type, expr)
  149.      tree type, expr;
  150. {
  151.   register tree intype = TREE_TYPE (expr);
  152.   register enum tree_code form = TREE_CODE (intype);
  153.   
  154.   if (integer_zerop (expr))
  155.     {
  156.       if (type == TREE_TYPE (null_pointer_node))
  157.     return null_pointer_node;
  158.       expr = build_int_2 (0, 0);
  159.       TREE_TYPE (expr) = type;
  160.       return expr;
  161.     }
  162.  
  163.   if (form == POINTER_TYPE)
  164.     {
  165.       intype = TYPE_MAIN_VARIANT (intype);
  166.  
  167.       if (TYPE_MAIN_VARIANT (type) != intype
  168.       && IS_AGGR_TYPE (TREE_TYPE (type)) && IS_AGGR_TYPE (TREE_TYPE (intype)))
  169.     {
  170.       enum tree_code code = PLUS_EXPR;
  171.       tree path, basetype;
  172.       int distance = get_base_distance (TREE_TYPE (type),
  173.                         TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
  174.       if (distance == -2)
  175.         {
  176.         ambig:
  177.           error_with_aggr_type (TREE_TYPE (type), "type `%s' is ambiguous baseclass of `%s'",
  178.                     TYPE_NAME_STRING (TREE_TYPE (intype)));
  179.           return error_mark_node;
  180.         }
  181.       if (distance == -1)
  182.         {
  183.           distance = get_base_distance (TREE_TYPE (intype),
  184.                         TYPE_MAIN_VARIANT (TREE_TYPE (type)), 0, &path);
  185.           if (distance == -2)
  186.         goto ambig;
  187.           if (distance < 0)
  188.         /* Doesn't need any special help from us.  */
  189.         return build1 (NOP_EXPR, type, expr);
  190.  
  191.           code = MINUS_EXPR;
  192.         }
  193.       return build_vbase_path (code, type, expr, path, 0);
  194.     }
  195.       return build1 (NOP_EXPR, type, expr);
  196.     }
  197.  
  198.   return convert_to_pointer (type, expr);
  199. }
  200.  
  201. /* We are passing something to a function which requires a reference.
  202.    The type we are interested in is in TYPE. The initial
  203.    value we have to begin with is in ARG.
  204.  
  205.    FLAGS controls how we manage visibility checking.  */
  206. static tree
  207. build_up_reference (type, arg, flags)
  208.      tree type, arg;
  209.      int flags;
  210. {
  211.   tree rval;
  212.   int literal_flag = 0;
  213.   tree argtype = TREE_TYPE (arg), basetype = argtype;
  214.   tree target_type = TREE_TYPE (type);
  215.  
  216.   assert (TREE_CODE (type) == REFERENCE_TYPE);
  217.   if (TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
  218.       && IS_AGGR_TYPE (argtype)
  219.       && IS_AGGR_TYPE (target_type))
  220.     {
  221.       basetype = get_base_type (target_type, TYPE_MAIN_VARIANT (argtype),
  222.                 (flags & LOOKUP_PROTECTED_OK) ? 3 : 2);
  223.       if ((flags & LOOKUP_PROTECT) && basetype == error_mark_node)
  224.     return error_mark_node;
  225.     }
  226.  
  227.   switch (TREE_CODE (arg))
  228.     {
  229.     case INDIRECT_REF:
  230.       /* This is a call to a constructor which did not know what it was
  231.      initializing until now: it needs to initialize a temporary.  */
  232.       if (TYPE_HAS_CONSTRUCTOR (arg))
  233.     {
  234.       tree temp = build_cplus_new (argtype, TREE_OPERAND (arg, 0));
  235.       TYPE_HAS_CONSTRUCTOR (arg) = 0;
  236.       return build_up_reference (type, temp, flags);
  237.     }
  238.       /* Let &* cancel out to simplify resulting code.
  239.          Also, throw away intervening NOP_EXPRs.  */
  240.       arg = TREE_OPERAND (arg, 0);
  241.       if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == REFERENCE_EXPR)
  242.     arg = TREE_OPERAND (arg, 0);
  243.  
  244.       rval = build1 (REFERENCE_EXPR, type, arg);
  245.       literal_flag = TREE_LITERAL (arg);
  246.       goto done;
  247.  
  248.       /* Get this out of a register if we happened to be in one by accident.
  249.      Also, build up references to non-lvalues it we must.  */
  250.       /* For &x[y], return (&) x+y */
  251.     case ARRAY_REF:
  252.       if (mark_addressable (TREE_OPERAND (arg, 0)) == 0)
  253.     return error_mark_node;
  254.       rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
  255.                   TREE_OPERAND (arg, 1));
  256.       TREE_TYPE (rval) = type;
  257.       if (TREE_LITERAL (TREE_OPERAND (arg, 1))
  258.       && staticp (TREE_OPERAND (arg, 0)))
  259.     TREE_LITERAL (rval) = 1;
  260.       return rval;
  261.  
  262.     case SCOPE_REF:
  263.       /* Could be a reference to a static member.  */
  264.       {
  265.     tree field = TREE_OPERAND (arg, 1);
  266.     if (TREE_STATIC (field))
  267.       {
  268.         rval = build1 (ADDR_EXPR, type, field);
  269.         literal_flag = 1;
  270.         goto done;
  271.       }
  272.       }
  273.       /* we should have farmed out member pointers above.  */
  274.       assert (0);
  275.  
  276.     case COMPONENT_REF:
  277.       rval = build_component_addr (arg, build_pointer_type (argtype),
  278.                    "attempt to make a reference to bit-field structure member `%s'");
  279.       rval = build1 (NOP_EXPR, type, rval);
  280.       literal_flag = staticp (TREE_OPERAND (arg, 0));
  281. #if 0
  282.       goto done_but_maybe_warn;
  283. #else
  284.       goto done;
  285. #endif
  286.  
  287.       /* Anything not already handled and not a true memory reference
  288.      needs to have a reference built up.  Do so silently for
  289.      things like integers and return values from function,
  290.      but complain if we need a reference to something declared
  291.      as `register'.  */
  292.  
  293.     case RESULT_DECL:
  294.       if (staticp (arg))
  295.     literal_flag = 1;
  296.       TREE_ADDRESSABLE (arg) = 1;
  297.       put_var_into_stack (arg);
  298.       break;
  299.  
  300.     case PARM_DECL:
  301.       if (arg == current_class_decl)
  302.     {
  303.       error ("address of `this' not available");
  304.       TREE_ADDRESSABLE (arg) = 1; /* so compiler doesn't die later */
  305.       put_var_into_stack (arg);
  306.       break;
  307.     }
  308.       /* Fall through.  */
  309.     case VAR_DECL:
  310.     case CONST_DECL:
  311.       if (TREE_REGDECL (arg) && !TREE_ADDRESSABLE (arg))
  312.     warning ("address needed to build reference for `%s', which is declared `register'",
  313.          IDENTIFIER_POINTER (DECL_NAME (arg)));
  314.       else if (staticp (arg))
  315.     literal_flag = 1;
  316.  
  317.       TREE_ADDRESSABLE (arg) = 1;
  318.       put_var_into_stack (arg);
  319.       break;
  320.  
  321.     case COMPOUND_EXPR:
  322.       {
  323.     tree real_reference = build_up_reference (type, TREE_OPERAND (arg, 1), 1);
  324.     rval = build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0), real_reference);
  325.     TREE_LITERAL (rval) = staticp (TREE_OPERAND (arg, 1));
  326.     return rval;
  327.       }
  328.  
  329.     case MODIFY_EXPR:
  330.     case INIT_EXPR:
  331.       {
  332.     tree real_reference = build_up_reference (type, TREE_OPERAND (arg, 0), 1);
  333.     rval = build (COMPOUND_EXPR, type, arg, real_reference);
  334.     TREE_LITERAL (rval) = staticp (TREE_OPERAND (arg, 0));
  335.     return rval;
  336.       }
  337.  
  338.     case COND_EXPR:
  339.       return build (COND_EXPR, type,
  340.             TREE_OPERAND (arg, 0),
  341.             build_up_reference (type, TREE_OPERAND (arg, 1), 1),
  342.             build_up_reference (type, TREE_OPERAND (arg, 2), 1));
  343.  
  344.     case WITH_CLEANUP_EXPR:
  345.       rval = build (WITH_CLEANUP_EXPR, type,
  346.             build_up_reference (type, TREE_OPERAND (arg, 0), 1),
  347.             0, TREE_OPERAND (arg, 2));
  348.       return rval;
  349.  
  350.     default:
  351.       break;
  352.     }
  353.  
  354.   if (TREE_ADDRESSABLE (arg) == 0)
  355.     {
  356.       tree temp;
  357.  
  358.       if (TREE_CODE (arg) == CALL_EXPR && IS_AGGR_TYPE (argtype))
  359.     {
  360.       temp = build_cplus_new (argtype, arg);
  361.       rval = build1 (ADDR_EXPR, type, temp);
  362.       goto done;
  363.     }
  364.       else
  365.     {
  366.       temp = get_temp_name (argtype, 0);
  367.       if (global_bindings_p ())
  368.         {
  369.           /* Give this new temp some rtl and initialize it.  */
  370.           DECL_INITIAL (temp) = arg;
  371.           TREE_STATIC (temp) = 1;
  372.           finish_decl (temp, arg, NULL_TREE);
  373.           /* Do this after declaring it static.  */
  374.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  375.           literal_flag = TREE_LITERAL (rval);
  376.           goto done;
  377.         }
  378.       else
  379.         {
  380.           rval = build_unary_op (ADDR_EXPR, temp, 0);
  381.           /* Put a value into the rtl.  */
  382.           if (IS_AGGR_TYPE (argtype))
  383.         {
  384.           /* This may produce surprising results,
  385.              since we commit to initializing the temp
  386.              when the temp may not actually get used.  */
  387.           expand_aggr_init (temp, arg, 0);
  388.           TREE_TYPE (rval) = type;
  389.           literal_flag = TREE_LITERAL (rval);
  390.           goto done;
  391.         }
  392.           else
  393.         {
  394.           if (basetype != argtype)
  395.             rval = convert_pointer_to (target_type, rval);
  396.           else
  397.             TREE_TYPE (rval) = type;
  398.           return build (COMPOUND_EXPR, type,
  399.                 build (MODIFY_EXPR, argtype, temp, arg), rval);
  400.         }
  401.         }
  402.     }
  403.     }
  404.   else
  405.     rval = build1 (ADDR_EXPR, type, arg);
  406.  
  407.  done_but_maybe_warn:
  408.   if (TREE_READONLY (arg)
  409.       && ! TREE_READONLY (target_type))
  410.     readonly_warning_or_error (arg, "conversion to reference");
  411.  
  412.  done:
  413.   if (TYPE_LANG_SPECIFIC (argtype)
  414.       && (TYPE_USES_MULTIPLE_INHERITANCE (argtype)
  415.       || TYPE_USES_VIRTUAL_BASECLASSES (argtype)))
  416.     {
  417.       TREE_TYPE (rval) = TYPE_POINTER_TO (argtype);
  418.       rval = convert_pointer_to (target_type, rval);
  419.       rval = build1 (NOP_EXPR, type, rval);
  420.     }
  421.   TREE_LITERAL (rval) = literal_flag;
  422.   return rval;
  423. }
  424.  
  425. /* For C++: Only need to do one-level references, but cannot
  426.    get tripped up on signed/unsigned differences.
  427.  
  428.    If DECL is NULL_TREE it means convert as though casting (by force).
  429.    If it is ERROR_MARK_NODE, it means the conversion is implicit,
  430.    and that temporaries may be created.
  431.    Otherwise, DECL is a _DECL node which can be used in error reporting.  */
  432. tree
  433. convert_to_reference (decl, reftype, expr, strict, flags)
  434.      tree decl;
  435.      tree reftype, expr;
  436.      int strict, flags;
  437. {
  438.   register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
  439.   register tree intype = TREE_TYPE (expr);
  440.   register enum tree_code form = TREE_CODE (intype);
  441.  
  442.   assert (TREE_CODE (reftype) == REFERENCE_TYPE);
  443.  
  444.   if (form == REFERENCE_TYPE)
  445.     intype = TREE_TYPE (intype);
  446.   intype = TYPE_MAIN_VARIANT (intype);
  447.  
  448.   /* @@ Probably need to have a check for X(X&) here.  */
  449.  
  450.   if (IS_AGGR_TYPE (intype))
  451.     {
  452.       tree rval = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
  453.       if (rval)
  454.     {
  455.       if (rval == error_mark_node)
  456.         error ("ambiguous pointer conversion");
  457.       return rval;
  458.     }
  459.       if (form == REFERENCE_TYPE
  460.       && type != intype
  461.       && TYPE_LANG_SPECIFIC (intype)
  462.       && (TYPE_USES_VIRTUAL_BASECLASSES (intype)
  463.           || TYPE_USES_MULTIPLE_INHERITANCE (intype)))
  464.     {
  465.       /* If it may move around, build a fresh reference.  */
  466.       expr = convert_from_reference (expr);
  467.       form = TREE_CODE (TREE_TYPE (expr));
  468.     }
  469.     }
  470.  
  471.   /* @@ Perhaps this should try to go through a constructor first
  472.      @@ for proper initialization, but I am not sure when that
  473.      @@ is needed or desirable.
  474.  
  475.      @@ The second disjunct is provided to make references behave
  476.      @@ as some people think they should, i.e., an interconvertability
  477.      @@ between references to builtin types.  I am beginning
  478.      @@ to think that this is wrong.  */
  479.  
  480.   if (((IS_AGGR_TYPE (type) || IS_AGGR_TYPE (intype))
  481.        && comptypes (type, intype, strict))
  482.       || (!IS_AGGR_TYPE (type)
  483.       && !IS_AGGR_TYPE (intype)
  484.       && int_size_in_bytes (type) <= int_size_in_bytes (intype)))
  485.     {
  486.       /* If EXPR is of aggregate type, and is really a CALL_EXPR,
  487.      then we don't need to convert it to reference type if
  488.      it is only being used to initialize DECL which is also
  489.      of the same aggregate type.  */
  490.       if (form == REFERENCE_TYPE
  491.       || (decl != NULL_TREE && decl != error_mark_node
  492.           && IS_AGGR_TYPE (type)
  493.           && TREE_CODE (expr) == CALL_EXPR
  494.           && TYPE_MAIN_VARIANT (type) == intype))
  495.     {
  496.       if (decl && decl != error_mark_node)
  497.         {
  498.           tree e1 = build (INIT_EXPR, void_type_node, decl, expr);
  499.           tree e2;
  500.  
  501.           TREE_VOLATILE (e1) = 1;
  502.           if (form == REFERENCE_TYPE)
  503.         e2 = build1 (NOP_EXPR, reftype, decl);
  504.           else
  505.         {
  506.           e2 = build_unary_op (ADDR_EXPR, decl, 0);
  507.           e2 = build1 (REFERENCE_EXPR, reftype, e2);
  508.         }
  509.           return build_compound_expr (tree_cons (NULL_TREE, e1,
  510.                              build_tree_list (NULL_TREE, e2)));
  511.         }
  512.       expr = copy_node (expr);
  513.       TREE_TYPE (expr) = reftype;
  514.       return expr;
  515.     }
  516.       if (decl == error_mark_node)
  517.     flags |= LOOKUP_PROTECTED_OK;
  518.       return build_up_reference (reftype, expr, flags);
  519.     }
  520.  
  521.   /* Definitely need to go through a constructor here.  */
  522.   if (TYPE_HAS_CONSTRUCTOR (type))
  523.     {
  524.       tree init = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, expr), CLASSTYPE_AS_LIST (type), LOOKUP_NORMAL);
  525.       tree rval;
  526.  
  527.       if (init == error_mark_node)
  528.     return error_mark_node;
  529.       rval = build_cplus_new (type, init);
  530.       if (decl == error_mark_node)
  531.     flags |= LOOKUP_PROTECTED_OK;
  532.       return build_up_reference (reftype, rval, flags);
  533.     }
  534.  
  535.   assert (form != OFFSET_TYPE);
  536.  
  537.   error ("cannot convert to a reference type");
  538.  
  539.   return error_mark_node;
  540. }
  541.  
  542. /* We are using a reference VAL for its value. Bash that reference all the
  543.    way down to its lowest form. */
  544. tree
  545. convert_from_reference (val)
  546.      tree val;
  547. {
  548.   tree type = TREE_TYPE (val);
  549.  
  550. #if 0
  551.   if (TREE_CODE (val) == REFERENCE_EXPR)
  552.     {
  553.       val = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)),
  554.             TREE_OPERAND (val, 0));
  555.       return val;
  556.     }
  557. #endif
  558.   if (TREE_CODE (type) == OFFSET_TYPE)
  559.     type = TREE_TYPE (type);
  560.  if (TREE_CODE (type) == REFERENCE_TYPE)
  561.     {
  562.       tree dt = TREE_TYPE (type);
  563.  
  564.       /* This can happen if we cast to a reference type.  */
  565.       if (TREE_CODE (val) == ADDR_EXPR)
  566.     {
  567.       val = build1 (NOP_EXPR, build_pointer_type (dt), val);
  568.       val = build_indirect_ref (val, 0);
  569.       return val;
  570.     }
  571.  
  572.       if (TREE_READONLY (type))
  573.     {
  574.       val = build_indirect_ref (val, "reference bashing (compiler error)");
  575.       if (TREE_CODE (val) == VAR_DECL && TREE_READONLY (val)
  576.           && DECL_INITIAL (val))
  577.         return DECL_INITIAL (val);
  578.     }
  579.  
  580.       val = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (dt), val);
  581.  
  582.       TREE_THIS_VOLATILE (val) = TREE_VOLATILE (dt);
  583.       TREE_READONLY (val) = TREE_READONLY (dt);
  584.     }
  585.   return val;
  586. }
  587.  
  588. static tree
  589. convert_to_real (type, expr)
  590.      tree type, expr;
  591. {
  592.   register enum tree_code form = TREE_CODE (TREE_TYPE (expr));
  593.   extern int flag_float_store;
  594.  
  595.   if (form == REAL_TYPE)
  596.     return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
  597.           type, expr);
  598.  
  599.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  600.     return build1 (FLOAT_EXPR, type, expr);
  601.  
  602.   assert (form != OFFSET_TYPE);
  603.  
  604.   if (form == POINTER_TYPE)
  605.     error ("pointer value used where a floating point value was expected");
  606.   /* C++: check to see if we can convert this aggregate type
  607.      into the required scalar type.  */
  608.   else if (IS_AGGR_TYPE (TREE_TYPE (expr)))
  609.     {
  610.       tree rval;
  611.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  612.       if (rval) return rval;
  613.     }
  614.   else
  615.     error ("aggregate value used where a floating point value was expected");
  616.  
  617.   {
  618.     register tree tem = make_node (REAL_CST);
  619.     TREE_TYPE (tem) = type;
  620.     TREE_REAL_CST (tem) = 0;
  621.     return tem;
  622.   }
  623. }
  624.  
  625. /* The result of this is always supposed to be a newly created tree node
  626.    not in use in any existing structure.  */
  627.  
  628. static tree
  629. convert_to_integer (type, expr)
  630.      tree type, expr;
  631. {
  632.   register tree intype = TREE_TYPE (expr);
  633.   register enum tree_code form = TREE_CODE (intype);
  634.   extern tree build_binary_op_nodefault ();
  635.   extern tree build_unary_op ();
  636.  
  637.   if (form == POINTER_TYPE)
  638.     {
  639.       if (integer_zerop (expr))
  640.     expr = integer_zero_node;
  641.       else
  642.     expr = fold (build1 (CONVERT_EXPR,
  643.                  type_for_size (POINTER_SIZE, 0), expr));
  644.       intype = TREE_TYPE (expr);
  645.       form = TREE_CODE (intype);
  646.       if (intype == type)
  647.     return expr;
  648.     }
  649.  
  650.   if (form == INTEGER_TYPE || form == ENUMERAL_TYPE)
  651.     {
  652.       register int outprec = TYPE_PRECISION (type);
  653.       register int inprec = TYPE_PRECISION (intype);
  654.       register enum tree_code ex_form = TREE_CODE (expr);
  655.  
  656.       if (outprec >= inprec)
  657.     return build1 (NOP_EXPR, type, expr);
  658.  
  659. /* Here detect when we can distribute the truncation down past some arithmetic.
  660.    For example, if adding two longs and converting to an int,
  661.    we can equally well convert both to ints and then add.
  662.    For the operations handled here, such truncation distribution
  663.    is always safe.
  664.    It is desirable in these cases:
  665.    1) when truncating down to full-word from a larger size
  666.    2) when truncating takes no work.
  667.    3) when at least one operand of the arithmetic has been extended
  668.    (as by C's default conversions).  In this case we need two conversions
  669.    if we do the arithmetic as already requested, so we might as well
  670.    truncate both and then combine.  Perhaps that way we need only one.
  671.  
  672.    Note that in general we cannot do the arithmetic in a type
  673.    shorter than the desired result of conversion, even if the operands
  674.    are both extended from a shorter type, because they might overflow
  675.    if combined in that type.  The exceptions to this--the times when
  676.    two narrow values can be combined in their narrow type even to
  677.    make a wider result--are handled by "shorten" in build_binary_op.  */
  678.  
  679.       switch (ex_form)
  680.     {
  681.     case RSHIFT_EXPR:
  682.       /* We can pass truncation down through right shifting
  683.          when the shift count is a negative constant.  */
  684.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  685.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) > 0)
  686.         break;
  687.       goto trunc1;
  688.  
  689.     case LSHIFT_EXPR:
  690.       /* We can pass truncation down through left shifting
  691.          when the shift count is a positive constant.  */
  692.       if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  693.           || TREE_INT_CST_LOW (TREE_OPERAND (expr, 1)) < 0)
  694.         break;
  695.       /* In this case, shifting is like multiplication.  */
  696.       goto trunc1;
  697.  
  698.     case MAX_EXPR:
  699.     case MIN_EXPR:
  700.     case MULT_EXPR:
  701.       {
  702.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  703.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  704.  
  705.         /* Don't distribute unless the output precision is at least as big
  706.            as the actual inputs.  Otherwise, the comparison of the
  707.            truncated values will be wrong.  */
  708.         if (outprec >= TYPE_PRECISION (TREE_TYPE (arg0))
  709.         && outprec >= TYPE_PRECISION (TREE_TYPE (arg1))
  710.         /* If signedness of arg0 and arg1 don't match,
  711.            we can't necessarily find a type to compare them in.  */
  712.         && (TREE_UNSIGNED (TREE_TYPE (arg0))
  713.             == TREE_UNSIGNED (TREE_TYPE (arg1))))
  714.           goto trunc1;
  715.         break;
  716.       }
  717.  
  718.     case PLUS_EXPR:
  719.     case MINUS_EXPR:
  720.     case BIT_AND_EXPR:
  721.     case BIT_IOR_EXPR:
  722.     case BIT_XOR_EXPR:
  723.     case BIT_ANDTC_EXPR:
  724.     trunc1:
  725.       {
  726.         tree arg0 = get_unwidened (TREE_OPERAND (expr, 0), type);
  727.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  728.  
  729.         if (outprec >= BITS_PER_WORD
  730.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  731.         || inprec > TYPE_PRECISION (TREE_TYPE (arg0))
  732.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1)))
  733.           {
  734.         /* Do the arithmetic in type TYPEX,
  735.            then convert result to TYPE.  */
  736.         register tree typex = type;
  737.  
  738.         /* Can't do arithmetic in enumeral types
  739.            so use an integer type that will hold the values.  */
  740.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  741.           typex = type_for_size (TYPE_PRECISION (typex),
  742.                      TREE_UNSIGNED (typex));
  743.  
  744.         /* But now perhaps TYPEX is as wide as INPREC.
  745.            In that case, do nothing special here.
  746.            (Otherwise would recurse infinitely in convert.  */
  747.         if (TYPE_PRECISION (typex) != inprec)
  748.           {
  749.             /* Don't do unsigned arithmetic where signed was wanted,
  750.                or vice versa.
  751.                Exception: if the original operands were unsigned
  752.                then can safely do the work as unsigned.
  753.                And we may need to do it as unsigned
  754.                if we truncate to the original size.  */
  755.             typex = ((TREE_UNSIGNED (TREE_TYPE (expr))
  756.                   || TREE_UNSIGNED (TREE_TYPE (arg0)))
  757.                  ? unsigned_type (typex) : signed_type (typex));
  758.             return convert (type,
  759.                     build_binary_op_nodefault (ex_form,
  760.                                    convert (typex, arg0),
  761.                                    convert (typex, arg1),
  762.                                    ex_form));
  763.           }
  764.           }
  765.       }
  766.       break;
  767.  
  768.     case EQ_EXPR:
  769.     case NE_EXPR:
  770.     case GT_EXPR:
  771.     case GE_EXPR:
  772.     case LT_EXPR:
  773.     case LE_EXPR:
  774.     case TRUTH_AND_EXPR:
  775.     case TRUTH_ANDIF_EXPR:
  776.     case TRUTH_OR_EXPR:
  777.     case TRUTH_ORIF_EXPR:
  778.     case TRUTH_NOT_EXPR:
  779.       /* If we want result of comparison converted to a byte,
  780.          we can just regard it as a byte, since it is 0 or 1.  */
  781.       TREE_TYPE (expr) = type;
  782.       return expr;
  783.  
  784.     case NEGATE_EXPR:
  785.     case BIT_NOT_EXPR:
  786.     case ABS_EXPR:
  787.       {
  788.         register tree typex = type;
  789.  
  790.         /* Can't do arithmetic in enumeral types
  791.            so use an integer type that will hold the values.  */
  792.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  793.           typex = type_for_size (TYPE_PRECISION (typex),
  794.                      TREE_UNSIGNED (typex));
  795.  
  796.         /* But now perhaps TYPEX is as wide as INPREC.
  797.            In that case, do nothing special here.
  798.            (Otherwise would recurse infinitely in convert.  */
  799.         if (TYPE_PRECISION (typex) != inprec)
  800.           {
  801.         /* Don't do unsigned arithmetic where signed was wanted,
  802.            or vice versa.  */
  803.         typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  804.              ? unsigned_type (typex) : signed_type (typex));
  805.         return convert (type,
  806.                 build_unary_op (ex_form,
  807.                         convert (typex, TREE_OPERAND (expr, 0)),
  808.                         1));
  809.           }
  810.       }
  811.  
  812.     case NOP_EXPR:
  813.       /* If truncating after truncating, might as well do all at once.
  814.          If truncating after extending, we may get rid of wasted work.  */
  815.       return convert (type, get_unwidened (TREE_OPERAND (expr, 0), type));
  816.  
  817.     case COND_EXPR:
  818.       /* Can treat the two alternative values like the operands
  819.          of an arithmetic expression.  */
  820.       {
  821.         tree arg1 = get_unwidened (TREE_OPERAND (expr, 1), type);
  822.         tree arg2 = get_unwidened (TREE_OPERAND (expr, 2), type);
  823.  
  824.         if (outprec >= BITS_PER_WORD
  825.         || TRULY_NOOP_TRUNCATION (outprec, inprec)
  826.         || inprec > TYPE_PRECISION (TREE_TYPE (arg1))
  827.         || inprec > TYPE_PRECISION (TREE_TYPE (arg2)))
  828.           {
  829.         /* Do the arithmetic in type TYPEX,
  830.            then convert result to TYPE.  */
  831.         register tree typex = type;
  832.  
  833.         /* Can't do arithmetic in enumeral types
  834.            so use an integer type that will hold the values.  */
  835.         if (TREE_CODE (typex) == ENUMERAL_TYPE)
  836.           typex = type_for_size (TYPE_PRECISION (typex),
  837.                      TREE_UNSIGNED (typex));
  838.  
  839.         /* But now perhaps TYPEX is as wide as INPREC.
  840.            In that case, do nothing special here.
  841.            (Otherwise would recurse infinitely in convert.  */
  842.         if (TYPE_PRECISION (typex) != inprec)
  843.           {
  844.             /* Don't do unsigned arithmetic where signed was wanted,
  845.                or vice versa.  */
  846.             typex = (TREE_UNSIGNED (TREE_TYPE (expr))
  847.                  ? unsigned_type (typex) : signed_type (typex));
  848.             return convert (type,
  849.                     build (COND_EXPR, typex,
  850.                        TREE_OPERAND (expr, 0),
  851.                        convert (typex, arg1),
  852.                        convert (typex, arg2)));
  853.           }
  854.           }
  855.       }
  856.  
  857.     }
  858.  
  859.       return build1 (NOP_EXPR, type, expr);
  860.     }
  861.  
  862.   if (form == REAL_TYPE)
  863.     return build1 (FIX_TRUNC_EXPR, type, expr);
  864.  
  865.   if (form == OFFSET_TYPE)
  866.     error_with_decl (TYPE_NAME (TYPE_OFFSET_BASETYPE (intype)),
  867.              "pointer-to-member expression object not composed with type `%s' object");
  868.   else
  869.     {
  870.       if (IS_AGGR_TYPE (intype))
  871.     {
  872.       tree rval;
  873.       rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
  874.       if (rval) return rval;
  875.     }
  876.  
  877.       error ("aggregate value used where an integer was expected");
  878.     }
  879.  
  880.   {
  881.     register tree tem = build_int_2 (0, 0);
  882.     TREE_TYPE (tem) = type;
  883.     return tem;
  884.   }
  885. }
  886.  
  887. /* See if there is a constructor of type TYPE which will convert
  888.    EXPR.  The reference manual seems to suggest (8.5.6) that we need
  889.    not worry about finding constructors for base classes, then converting
  890.    to the derived class.
  891.  
  892.    MSGP is a pointer to a message that would be an appropriate error
  893.    string.  If MSGP is NULL, then we are not interested in reporting
  894.    errors.  */
  895. tree
  896. convert_to_aggr (type, expr, msgp, protect)
  897.      tree type, expr;
  898.      char **msgp;
  899. {
  900.   tree basetype = TYPE_MAIN_VARIANT (type);
  901.   tree name = DECL_NAME (TYPE_NAME (basetype));
  902.   tree field;
  903.   tree function, fntype, parmtypes, parmlist, result;
  904.   tree method_name;
  905.   enum visibility_type visibility;
  906.   int can_be_private, can_be_protected;
  907.  
  908.   if (! TYPE_HAS_CONSTRUCTOR (basetype))
  909.     {
  910.       if (msgp)
  911.     *msgp = "type `%s' does not have a constructor";
  912.       return error_mark_node;
  913.     }
  914.  
  915.   visibility = visibility_public;
  916.   can_be_private = 0;
  917.   can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
  918.  
  919.   parmlist = build_tree_list (NULL_TREE, expr);
  920.   parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
  921.  
  922.   if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
  923.     {
  924.       parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
  925.       parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
  926.     }
  927.  
  928.   /* The type of the first argument will be filled in inside the loop.  */
  929.   parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
  930.   parmtypes = tree_cons (NULL_TREE, TYPE_POINTER_TO (basetype), parmtypes);
  931.  
  932.   method_name = build_decl_overload (IDENTIFIER_POINTER (name), parmtypes, 1);
  933.  
  934.   /* constructors are up front.  */
  935.   field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  936.   if (TYPE_HAS_DESTRUCTOR (basetype))
  937.     field = TREE_CHAIN (field);
  938.  
  939.   while (field)
  940.     {
  941.       if (DECL_NAME (field) == method_name)
  942.     {
  943.       function = field;
  944.       if (protect)
  945.         {
  946.           if (TREE_PRIVATE (field))
  947.         {
  948.           can_be_private =
  949.             (basetype == current_class_type
  950.              || is_friend (basetype, current_function_decl)
  951.              || purpose_member (basetype, DECL_VISIBILITY (field)));
  952.           if (! can_be_private)
  953.             goto found;
  954.         }
  955.           else if (TREE_PROTECTED (field))
  956.         {
  957.           if (! can_be_protected)
  958.             goto found;
  959.         }
  960.         }
  961.       goto found_and_ok;
  962.     }
  963.       field = TREE_CHAIN (field);
  964.     }
  965.  
  966.   /* No exact conversion was found.  See if an approximate
  967.      one will do.  */
  968.   field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
  969.   if (TYPE_HAS_DESTRUCTOR (basetype))
  970.     field = TREE_CHAIN (field);
  971.  
  972.   {
  973.     int saw_private = 0;
  974.     int saw_protected = 0;
  975.     struct candidate *candidates =
  976.       (struct candidate *) alloca ((list_length (field)+1) * sizeof (struct candidate));
  977.     struct candidate *cp = candidates;
  978.  
  979.     while (field)
  980.       {
  981.     function = field;
  982.     cp->harshness = (unsigned short *)alloca (3 * sizeof (short));
  983.     compute_conversion_costs (function, parmlist, cp, 2);
  984.     if (cp->evil == 0)
  985.       {
  986.         cp->u.field = field;
  987.         if (protect)
  988.           {
  989.         if (TREE_PRIVATE (field))
  990.           visibility = visibility_private;
  991.         else if (TREE_PROTECTED (field))
  992.           visibility = visibility_protected;
  993.         else
  994.           visibility = visibility_public;
  995.           }
  996.         else
  997.           visibility = visibility_public;
  998.  
  999.         if (visibility == visibility_private
  1000.         ? (basetype == current_class_type
  1001.            || is_friend (basetype, cp->function)
  1002.            || purpose_member (basetype, DECL_VISIBILITY (field)))
  1003.         : visibility == visibility_protected
  1004.         ? (can_be_protected
  1005.            || purpose_member (basetype, DECL_VISIBILITY (field)))
  1006.         : 1)
  1007.           {
  1008.         if (cp->user == 0 && cp->b_or_d == 0
  1009.             && cp->easy <= 1)
  1010.           {
  1011.             goto found_and_ok;
  1012.           }
  1013.         cp++;
  1014.           }
  1015.         else
  1016.           {
  1017.         if (visibility == visibility_private)
  1018.           saw_private = 1;
  1019.         else
  1020.           saw_protected = 1;
  1021.           }
  1022.       }
  1023.     field = TREE_CHAIN (field);
  1024.       }
  1025.     if (cp - candidates)
  1026.       {
  1027.     /* Rank from worst to best.  Then cp will point to best one.
  1028.        Private fields have their bits flipped.  For unsigned
  1029.        numbers, this should make them look very large.
  1030.        If the best alternate has a (signed) negative value,
  1031.        then all we ever saw were private members.  */
  1032.     if (cp - candidates > 1)
  1033.       qsort (candidates,    /* char *base */
  1034.          cp - candidates, /* int nel */
  1035.          sizeof (struct candidate), /* int width */
  1036.          rank_for_overload); /* int (*compar)() */
  1037.  
  1038.     --cp;
  1039.     if (cp->evil > 1)
  1040.       {
  1041.         if (msgp)
  1042.           *msgp = "ambiguous type conversion possible for `%s'";
  1043.         return error_mark_node;
  1044.       }
  1045.  
  1046.     function = cp->function;
  1047.     field = cp->u.field;
  1048.     goto found_and_ok;
  1049.       }
  1050.     else if (msgp)
  1051.       {
  1052.     if (saw_private)
  1053.       if (saw_protected)
  1054.         *msgp = "only private and protected conversions apply";
  1055.       else
  1056.         *msgp = "only private conversions apply";
  1057.     else if (saw_protected)
  1058.       *msgp = "only protected conversions apply";
  1059.       }
  1060.     return error_mark_node;
  1061.   }
  1062.   /* NOTREACHED */
  1063.  
  1064.  not_found:
  1065.   if (msgp) *msgp = "no appropriate conversion to type `%s'";
  1066.   return error_mark_node;
  1067.  found:
  1068.   if (visibility == visibility_private)
  1069.     if (! can_be_private)
  1070.       {
  1071.     if (msgp)
  1072.       *msgp = TREE_PRIVATE (field)
  1073.         ? "conversion to type `%s' is private"
  1074.         : "conversion to type `%s' is from private base class";
  1075.     return error_mark_node;
  1076.       }
  1077.   if (visibility == visibility_protected)
  1078.     if (! can_be_protected)
  1079.       {
  1080.     if (msgp)
  1081.       *msgp = TREE_PRIVATE (field)
  1082.         ? "conversion to type `%s' is protected"
  1083.         : "conversion to type `%s' is from protected base class";
  1084.     return error_mark_node;
  1085.       }
  1086.   function = field;
  1087.  found_and_ok:
  1088.  
  1089.   /* It will convert, but we don't do anything about it yet.  */
  1090.   if (msgp == 0)
  1091.     return NULL_TREE;
  1092.  
  1093.   fntype = TREE_TYPE (function);
  1094.   if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
  1095.     function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
  1096.   else
  1097.     function = default_conversion (function);
  1098.  
  1099.   result = build_nt (CALL_EXPR, function,
  1100.              actualparameterlist (NULL_TREE, TYPE_ARG_TYPES (fntype), parmlist, NULL_TREE, LOOKUP_NORMAL),
  1101.              NULL_TREE);
  1102.   TREE_TYPE (result) = TREE_TYPE (fntype);
  1103.   TREE_VOLATILE (result) = 1;
  1104.   TREE_RAISES (result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
  1105.   return result;
  1106. }
  1107.  
  1108. /* Call this when we know (for any reason) that expr is
  1109.    not, in fact, zero.  */
  1110. tree
  1111. convert_pointer_to (type, expr)
  1112.      tree type, expr;
  1113. {
  1114.   register tree intype = TREE_TYPE (expr);
  1115.   register enum tree_code form = TREE_CODE (intype);
  1116.   tree ptr_type = build_pointer_type (type);
  1117.   tree rval;
  1118.  
  1119.   if (TYPE_MAIN_VARIANT (ptr_type) == TYPE_MAIN_VARIANT (intype))
  1120.     return expr;
  1121.  
  1122.   if (intype == error_mark_node)
  1123.     return error_mark_node;
  1124.  
  1125.   assert (form == POINTER_TYPE);
  1126.   assert (!integer_zerop (expr));
  1127.  
  1128.   if (IS_AGGR_TYPE (type)
  1129.       && IS_AGGR_TYPE (TREE_TYPE (intype))
  1130.       && TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
  1131.     {
  1132.       tree path, basetype;
  1133.       int distance = get_base_distance (type, TYPE_MAIN_VARIANT (TREE_TYPE (intype)), 0, &path);
  1134.  
  1135.       /* This function shouldn't be called with
  1136.      unqualified arguments.  */
  1137.       assert (distance >= 0);
  1138.  
  1139.       return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
  1140.     }
  1141.   rval = build1 (NOP_EXPR, ptr_type,
  1142.          TREE_CODE (expr) == NOP_EXPR
  1143.          ? TREE_OPERAND (expr, 0) : expr);
  1144.   TREE_LITERAL (rval) = TREE_LITERAL (expr);
  1145.   return rval;
  1146. }
  1147.  
  1148. /* Create an expression whose value is that of EXPR,
  1149.    converted to type TYPE.  The TREE_TYPE of the value
  1150.    is always TYPE.  This function implements all reasonable
  1151.    conversions; callers should filter out those that are
  1152.    not permitted by the language being compiled.  */
  1153.  
  1154. tree
  1155. convert (type, expr)
  1156.      tree type, expr;
  1157. {
  1158.   register tree e = expr;
  1159.   register enum tree_code code = TREE_CODE (type);
  1160.  
  1161.   if (type == TREE_TYPE (expr) || TREE_CODE (expr) == ERROR_MARK)
  1162.     return expr;
  1163.   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  1164.     return error_mark_node;
  1165.   if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
  1166.     {
  1167.       error ("void value not ignored as it ought to be");
  1168.       return error_mark_node;
  1169.     }
  1170.   if (code == VOID_TYPE)
  1171.     {
  1172.       tree rval = build_type_conversion (NOP_EXPR, type, e, 0);
  1173.       /* If we can convert to void type via a type conversion, do so.  */
  1174.       if (rval)
  1175.     return rval;
  1176.       return build1 (CONVERT_EXPR, type, e);
  1177.     }
  1178. #if 0
  1179.   /* This is incorrect.  A truncation can't be stripped this way.
  1180.      Extensions will be stripped by the use of get_unwidened.  */
  1181.   if (TREE_CODE (expr) == NOP_EXPR)
  1182.     return convert (type, TREE_OPERAND (expr, 0));
  1183. #endif
  1184.  
  1185.   /* Just convert to the type of the member.  */
  1186.   if (code == OFFSET_TYPE)
  1187.     {
  1188.       type = TREE_TYPE (type);
  1189.       code = TREE_CODE (type);
  1190.     }
  1191.  
  1192.   /* C++ */
  1193.   if (code == REFERENCE_TYPE)
  1194.     return fold (convert_to_reference (error_mark_node, type, e, -1, LOOKUP_NORMAL));
  1195.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1196.     e = convert_from_reference (e);
  1197.  
  1198.   if (code == INTEGER_TYPE || code == ENUMERAL_TYPE)
  1199.     return fold (convert_to_integer (type, e));
  1200.   if (code == POINTER_TYPE)
  1201.     return fold (convert_to_pointer (type, e));
  1202.   if (code == REAL_TYPE)
  1203.     return fold (convert_to_real (type, e));
  1204.  
  1205.   /* New C++ semantics:  since assignment is now based on
  1206.      memberwise copying,  if the rhs type is derived from the
  1207.      lhs type, then we may still do a conversion.  */
  1208.   if (IS_AGGR_TYPE_CODE (code))
  1209.     {
  1210.       tree dtype = TREE_TYPE (e);
  1211.  
  1212.       if (TREE_CODE (dtype) == REFERENCE_TYPE)
  1213.     {
  1214.       e = convert_from_reference (e);
  1215.       dtype = TREE_TYPE (e);
  1216.     }
  1217.       dtype = TYPE_MAIN_VARIANT (dtype);
  1218.  
  1219.       /* Conversion between aggregate types.  New C++ semantics allow
  1220.      objects of derived type to be cast to objects of base type.
  1221.      Old semantics only allowed this bwteen pointers.
  1222.  
  1223.      There may be some ambiguity between using a constructor
  1224.      vs. using a type conversion operator when both apply.  */
  1225.  
  1226.       if (IS_AGGR_TYPE (dtype))
  1227.     {
  1228.       tree basetype;
  1229.  
  1230.       tree conversion = TYPE_HAS_CONVERSION (dtype)
  1231.         ? build_type_conversion (CONVERT_EXPR, type, e, 1) : NULL_TREE;
  1232.  
  1233.       if (TYPE_HAS_CONSTRUCTOR (type))
  1234.         {
  1235.           tree rval = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, e), CLASSTYPE_AS_LIST (type),
  1236.                          conversion ? LOOKUP_NO_CONVERSION : 0);
  1237.  
  1238.           if (rval != error_mark_node)
  1239.         {
  1240.           if (conversion)
  1241.             {
  1242.               error ("both constructor and type conversion operator apply");
  1243.               return error_mark_node;
  1244.             }
  1245.           /* call to constructor successful.  */
  1246.           rval = build_cplus_new (type, rval);
  1247.           return rval;
  1248.         }
  1249.         }
  1250.       /* Type conversion successful/applies.  */
  1251.       if (conversion)
  1252.         {
  1253.           if (conversion == error_mark_node)
  1254.         error ("ambiguous pointer conversion");
  1255.           return conversion;
  1256.         }
  1257.  
  1258.       /* now try normal C++ assignment semantics.  */
  1259.       basetype = dtype;
  1260.       if (type == basetype
  1261.           || (basetype = get_base_type (type, dtype, 1)))
  1262.         {
  1263.           if (basetype == error_mark_node)
  1264.         return error_mark_node;
  1265.  
  1266. #if 0
  1267.           if (TYPE_VIRTUAL_P (type))
  1268.         warning ("assignment to virtual aggregate type");
  1269. #endif
  1270.           return build (COMPONENT_REF, type, e, TYPE_NAME (basetype));
  1271.         }
  1272.       error ("conversion between incompatible aggregate types requested");
  1273.       return error_mark_node;
  1274.     }
  1275.       /* conversion from non-aggregate to aggregate type requires constructor.  */
  1276.       else if (TYPE_HAS_CONSTRUCTOR (type))
  1277.     {
  1278.       tree rval;
  1279.       tree init = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, e), CLASSTYPE_AS_LIST (type), LOOKUP_NORMAL);
  1280.       if (init == error_mark_node)
  1281.         {
  1282.           error_with_aggr_type (type, "in conversion to type `%s'");
  1283.           return error_mark_node;
  1284.         }
  1285.       rval = build_cplus_new (type, init);
  1286.       return rval;
  1287.     }
  1288.     }
  1289.  
  1290.   error ("conversion to non-scalar type requested");
  1291.   return error_mark_node;
  1292. }
  1293.  
  1294. /* Like convert, except permit conversions to take place which
  1295.    are not normally allowed due to visibility restrictions
  1296.    (such as conversion from sub-type to private super-type).  */
  1297. tree
  1298. convert_force (type, expr)
  1299.      tree type;
  1300.      tree expr;
  1301. {
  1302.   register tree e = expr;
  1303.   register enum tree_code code = TREE_CODE (type);
  1304.  
  1305.   if (code == REFERENCE_TYPE)
  1306.     return fold (convert_to_reference (0, type, e, -1, 0));
  1307.   else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
  1308.     e = convert_from_reference (e);
  1309.  
  1310.   if (code == POINTER_TYPE)
  1311.     return fold (convert_to_pointer_force (type, e));
  1312.   return convert (type, e);
  1313. }
  1314.  
  1315. /* Subroutine of build_type_conversion.  */
  1316. static tree
  1317. build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
  1318.      tree xtype, basetype;
  1319.      tree expr;
  1320.      tree typename;
  1321.      int for_sure;
  1322. {
  1323.   tree first_arg = expr;
  1324.   tree rval;
  1325.   int flags;
  1326.  
  1327.   if (for_sure == 0)
  1328.     {
  1329.       if (! lvalue_p (expr))
  1330.     first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1331.       flags = LOOKUP_PROTECT;
  1332.     }
  1333.   else
  1334.     flags = LOOKUP_NORMAL;
  1335.  
  1336.   rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
  1337.   if (rval == error_mark_node)
  1338.     {
  1339.       if (for_sure == 0)
  1340.     return NULL_TREE;
  1341.       return error_mark_node;
  1342.     }
  1343.   if (first_arg != expr)
  1344.     {
  1345.       expr = build_up_reference (build_reference_type (TREE_TYPE (expr)), expr, 0);
  1346.       TREE_VALUE (TREE_OPERAND (rval, 1)) = build_unary_op (ADDR_EXPR, expr, 0);
  1347.     }
  1348.   if (TREE_CODE (TREE_TYPE (rval)) == REFERENCE_TYPE
  1349.       && TREE_CODE (xtype) != REFERENCE_TYPE)
  1350.     rval = default_conversion (rval);
  1351.   return convert (xtype, rval);
  1352. }
  1353.  
  1354. /* Convert an aggregate EXPR to type XTYPE.  If a conversion
  1355.    exists, return the attempted conversion.  This may
  1356.    return ERROR_MARK_NODE if the conversion is not
  1357.    allowed (references private members, etc).
  1358.    If no conversion exists, NULL_TREE is returned.
  1359.  
  1360.    If (FOR_SURE & 1) is non-zero, then we allow this type conversion
  1361.    to take place immediately.  Otherwise, we build a SAVE_EXPR
  1362.    which can be evaluated if the results are ever needed.
  1363.  
  1364.    If FOR_SURE >= 2, then we only look for exact conversions.
  1365.  
  1366.    TYPE may be a reference type, in which case we first look
  1367.    for something that will convert to a reference type.  If
  1368.    that fails, we will try to look for something of the
  1369.    reference's target type, and then return a reference to that.  */
  1370. tree
  1371. build_type_conversion (code, xtype, expr, for_sure)
  1372.      enum tree_code code;
  1373.      tree xtype, expr;
  1374.      int for_sure;
  1375. {
  1376.   /* C++: check to see if we can convert this aggregate type
  1377.      into the required scalar type.  */
  1378.   tree type, type_default;
  1379.   tree typename = build_typename_overload (xtype), *typenames;
  1380.   int n_variants = 0;
  1381.   tree basetype = TREE_TYPE (expr), save_basetype;
  1382.   tree rval;
  1383.   int exact_conversion = for_sure >= 2;
  1384.   for_sure &= 1;
  1385.  
  1386.   if (TREE_CODE (basetype) == REFERENCE_TYPE)
  1387.     basetype = TREE_TYPE (basetype);
  1388.  
  1389.   basetype = TYPE_MAIN_VARIANT (basetype);
  1390.   if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
  1391.     return 0;
  1392.  
  1393.   if (TREE_CODE (xtype) == POINTER_TYPE
  1394.       || TREE_CODE (xtype) == REFERENCE_TYPE)
  1395.     {
  1396.       /* Prepare to match a variant of this type.  */
  1397.       type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1398.       for (n_variants = 0; type; type = TYPE_NEXT_VARIANT (type))
  1399.     n_variants++;
  1400.       typenames = (tree *)alloca (n_variants * sizeof (tree));
  1401.       for (n_variants = 0, type = TYPE_MAIN_VARIANT (TREE_TYPE (xtype));
  1402.        type; n_variants++, type = TYPE_NEXT_VARIANT (type))
  1403.     {
  1404.       if (type == TREE_TYPE (xtype))
  1405.         typenames[n_variants] = typename;
  1406.       else if (TREE_CODE (xtype) == POINTER_TYPE)
  1407.         typenames[n_variants] = build_typename_overload (build_pointer_type (type));
  1408.       else
  1409.         typenames[n_variants] = build_typename_overload (build_reference_type (type));
  1410.     }
  1411.     }
  1412.  
  1413.   save_basetype = basetype;
  1414.   type = xtype;
  1415.  
  1416.   while (TYPE_HAS_CONVERSION (basetype))
  1417.     {
  1418.       int i;
  1419.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1420.     return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1421.       for (i = 0; i < n_variants; i++)
  1422.     if (typenames[i] != typename
  1423.         && lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typenames[i], 0))
  1424.       return build_type_conversion_1 (xtype, basetype, expr, typenames[i], for_sure);
  1425.  
  1426.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1427.     basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1428.       else break;
  1429.     }
  1430.  
  1431.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1432.     {
  1433.       tree first_arg = expr;
  1434.       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  1435.       basetype = save_basetype;
  1436.  
  1437.       /* May need to build a temporary for this.  */
  1438.       while (TYPE_HAS_CONVERSION (basetype))
  1439.     {
  1440.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1441.         {
  1442.           int flags;
  1443.  
  1444.           if (for_sure == 0)
  1445.         {
  1446.           if (! lvalue_p (expr))
  1447.             first_arg = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node);
  1448.           flags = LOOKUP_PROTECT;
  1449.         }
  1450.           else
  1451.         flags = LOOKUP_NORMAL;
  1452.           rval = build_method_call (first_arg, typename, NULL_TREE, NULL_TREE, flags);
  1453.           if (rval == error_mark_node)
  1454.         {
  1455.           if (for_sure == 0)
  1456.             return NULL_TREE;
  1457.           return error_mark_node;
  1458.         }
  1459.           TREE_VALUE (TREE_OPERAND (rval, 1)) = expr;
  1460.  
  1461.           if (IS_AGGR_TYPE (type))
  1462.         {
  1463.           tree init = build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), build_tree_list (NULL_TREE, rval), NULL_TREE, LOOKUP_NORMAL);
  1464.           tree temp = build_cplus_new (type, init);
  1465.           return build_up_reference (TYPE_REFERENCE_TO (type), temp, 0);
  1466.         }
  1467.           return convert (xtype, rval);
  1468.         }
  1469.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1470.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1471.       else break;
  1472.     }
  1473.       /* No free conversions for reference types, right?.  */
  1474.       return NULL_TREE;
  1475.     }
  1476.  
  1477.   if (exact_conversion)
  1478.     return NULL_TREE;
  1479.  
  1480.   /* No perfect match found, try default.  */
  1481.   if (TREE_CODE (type) == POINTER_TYPE)
  1482.     type_default = ptr_type_node;
  1483.   else if (type == void_type_node)
  1484.     return NULL_TREE;
  1485.   else
  1486.     {
  1487.       extern tree default_conversion ();
  1488.       tree tmp = default_conversion (build1 (NOP_EXPR, type, integer_zero_node));
  1489.       if (tmp == error_mark_node)
  1490.     return NULL_TREE;
  1491.       type_default = TREE_TYPE (tmp);
  1492.     }
  1493.  
  1494.   if (type_default != type)
  1495.     {
  1496.       type = type_default;
  1497.       typename = build_typename_overload (type);
  1498.       basetype = save_basetype;
  1499.  
  1500.       while (TYPE_HAS_CONVERSION (basetype))
  1501.     {
  1502.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1503.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1504.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1505.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1506.       else break;
  1507.     }
  1508.     }
  1509.   else if (type == ptr_type_node)
  1510.     {
  1511.       /* Try converting to some other pointer type
  1512.      with which void* is compatible.  */
  1513.       basetype = save_basetype;
  1514.  
  1515.       while (TYPE_HAS_CONVERSION (basetype))
  1516.     {
  1517.       if (CLASSTYPE_PTR_CONVERSION (basetype) != 0)
  1518.         {
  1519.           if (CLASSTYPE_PTR_CONVERSION (basetype) == error_mark_node)
  1520.         return error_mark_node;
  1521.           typename = DECL_ORIGINAL_NAME (CLASSTYPE_PTR_CONVERSION (basetype));
  1522.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1523.         }
  1524.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1525.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1526.       else break;
  1527.     }
  1528.     }
  1529.   else if (TREE_CODE (type) == POINTER_TYPE
  1530.        && TREE_READONLY (TREE_TYPE (type))
  1531.        && TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node)
  1532.     {
  1533.       /* Try converting to some other pointer type
  1534.      with which const void* is compatible.  */
  1535.       basetype = save_basetype;
  1536.  
  1537.       while (TYPE_HAS_CONVERSION (basetype))
  1538.     {
  1539.       if (CLASSTYPE_CONSTPTR_CONVERSION (basetype) != 0)
  1540.         {
  1541.           if (CLASSTYPE_CONSTPTR_CONVERSION (basetype) == error_mark_node)
  1542.         return error_mark_node;
  1543.           typename = DECL_ORIGINAL_NAME (CLASSTYPE_CONSTPTR_CONVERSION (basetype));
  1544.           return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1545.         }
  1546.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1547.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1548.       else break;
  1549.     }
  1550.     }
  1551.  
  1552.   /* Default promotion yields no new alternatives, try
  1553.      conversions which are anti-default, such as
  1554.  
  1555.      double -> float or int -> unsigned or unsigned -> long
  1556.  
  1557.      */
  1558.   if (type_default == type)
  1559.     {
  1560.       int not_again = 0;
  1561.  
  1562.       if (type == double_type_node)
  1563.     typename = build_typename_overload (float_type_node);
  1564.       else if (type == integer_type_node)
  1565.     typename = build_typename_overload (unsigned_type_node);
  1566.       else if (type == unsigned_type_node)
  1567.     typename = build_typename_overload (long_integer_type_node);
  1568.  
  1569.     again:
  1570.       basetype = save_basetype;
  1571.       while (TYPE_HAS_CONVERSION (basetype))
  1572.     {
  1573.       if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1574.         return build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1575.       if (CLASSTYPE_N_BASECLASSES (basetype))
  1576.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1577.       else break;
  1578.     }
  1579.       if (! not_again && type == integer_type_node)
  1580.     {
  1581.       typename = build_typename_overload (long_integer_type_node);
  1582.       not_again = 1;
  1583.       goto again;
  1584.     }
  1585.     }
  1586.  
  1587.   /* Now, try C promotions...
  1588.  
  1589.      float -> int
  1590.      int -> float, void *
  1591.      void * -> int
  1592.  
  1593.      Truthvalue conversions let us try to convert
  1594.      to pointer if we were going for int, and to int
  1595.      if we were looking for pointer.  */
  1596.  
  1597.     basetype = save_basetype;
  1598.     if (TREE_CODE (type) == REAL_TYPE
  1599.     || (TREE_CODE (type) == POINTER_TYPE
  1600.         && (code == TRUTH_ANDIF_EXPR
  1601.         || code == TRUTH_ORIF_EXPR
  1602.         || code == TRUTH_NOT_EXPR)))
  1603.       type = integer_type_node;
  1604.     else if (TREE_CODE (type) == INTEGER_TYPE)
  1605.       if (TYPE_HAS_REAL_CONVERSION (basetype))
  1606.     type = double_type_node;
  1607.       else if (code == TRUTH_ANDIF_EXPR
  1608.            || code == TRUTH_ORIF_EXPR
  1609.            || code == TRUTH_NOT_EXPR)
  1610.     type = ptr_type_node;
  1611.       else
  1612.     return NULL_TREE;
  1613.     else
  1614.       return NULL_TREE;
  1615.  
  1616.     typename = build_typename_overload (type);
  1617.     while (TYPE_HAS_CONVERSION (basetype))
  1618.       {
  1619.     if (lookup_fnfields (CLASSTYPE_AS_LIST (basetype), typename, 0))
  1620.       {
  1621.         rval = build_type_conversion_1 (xtype, basetype, expr, typename, for_sure);
  1622.         return rval;
  1623.       }
  1624.     if (CLASSTYPE_N_BASECLASSES (basetype))
  1625.       basetype = CLASSTYPE_BASECLASS (basetype, 1);
  1626.     else
  1627.       break;
  1628.       }
  1629.  
  1630.   return NULL_TREE;
  1631. }
  1632.  
  1633. /* Must convert two aggregate types to non-aggregate type.
  1634.    Attempts to find a non-ambiguous, "best" type conversion.
  1635.  
  1636.    Return 1 on success, 0 on failure.
  1637.  
  1638.    @@ What are the real semantics of this supposed to be??? */
  1639. int
  1640. build_default_binary_type_conversion (code, arg1, arg2)
  1641.      enum tree_code code;
  1642.      tree *arg1, *arg2;
  1643. {
  1644.   tree type1 = TREE_TYPE (*arg1);
  1645.   tree type2 = TREE_TYPE (*arg2);
  1646.   char *name1, *name2;
  1647.  
  1648.   if (TREE_CODE (TYPE_NAME (type1)) != TYPE_DECL)
  1649.     {
  1650.       tree decl = typedecl_for_tag (type1);
  1651.       if (decl)
  1652.     error ("type conversion nonexistant for type `%s'",
  1653.            IDENTIFIER_POINTER (DECL_NAME (decl)));
  1654.       else
  1655.     error ("type conversion nonexistant for non-C++ type");
  1656.       return 0;
  1657.     }
  1658.   if (TREE_CODE (TYPE_NAME (type2)) != TYPE_DECL)
  1659.     {
  1660.       tree decl = typedecl_for_tag (type2);
  1661.       if (decl)
  1662.     error ("type conversion nonexistant for type `%s'",
  1663.            IDENTIFIER_POINTER (decl));
  1664.       else
  1665.     error ("type conversion nonexistant for non-C++ type");
  1666.       return 0;
  1667.     }
  1668.  
  1669.   name1 = TYPE_NAME_STRING (type1);
  1670.   name2 = TYPE_NAME_STRING (type2);
  1671.  
  1672.   if (! TYPE_HAS_CONVERSION (type1))
  1673.     {
  1674.       if (! TYPE_HAS_CONVERSION (type2))
  1675.     error ("type conversion required for types `%s' and `%s'",
  1676.            name1, name2);
  1677.       else
  1678.     error ("type conversion required for type `%s'", name1);
  1679.       return 0;
  1680.     }
  1681.   else if (! TYPE_HAS_CONVERSION (type2))
  1682.     {
  1683.       error ("type conversion required for type `%s'", name2);
  1684.       return 0;
  1685.     }
  1686.  
  1687.   if (TYPE_HAS_INT_CONVERSION (type1) && TYPE_HAS_REAL_CONVERSION (type1))
  1688.     warning ("ambiguous type conversion for type `%s', defaulting to int", name1);
  1689.   if (TYPE_HAS_INT_CONVERSION (type1))
  1690.     {
  1691.       *arg1 = build_type_conversion (code, integer_type_node, *arg1, 1);
  1692.       *arg2 = build_type_conversion (code, integer_type_node, *arg2, 1);
  1693.     }
  1694.   else if (TYPE_HAS_REAL_CONVERSION (type1))
  1695.     {
  1696.       *arg1 = build_type_conversion (code, double_type_node, *arg1, 1);
  1697.       *arg2 = build_type_conversion (code, double_type_node, *arg2, 1);
  1698.     }
  1699.   else
  1700.     {
  1701.       *arg1 = build_type_conversion (code, ptr_type_node, *arg1, 1);
  1702.       if (*arg1 == error_mark_node)
  1703.     error ("ambiguous pointer conversion");
  1704.       *arg2 = build_type_conversion (code, ptr_type_node, *arg2, 1);
  1705.       if (*arg1 != error_mark_node && *arg2 == error_mark_node)
  1706.     error ("ambiguous pointer conversion");
  1707.     }
  1708.   if (*arg1 == 0)
  1709.     {
  1710.       if (*arg2 == 0 && type1 != type2)
  1711.     error ("default type conversion for types `%s' and `%s' failed",
  1712.            name1, name2);
  1713.       else
  1714.     error ("default type conversion for type `%s' failed", name1);
  1715.       return 0;
  1716.     }
  1717.   else if (*arg2 == 0)
  1718.     {
  1719.       error ("default type conversion for type `%s' failed", name2);
  1720.       return 0;
  1721.     }
  1722.   return 1;
  1723. }
  1724.  
  1725. /* Must convert two aggregate types to non-aggregate type.
  1726.    Attempts to find a non-ambiguous, "best" type conversion.
  1727.  
  1728.    Return 1 on success, 0 on failure.
  1729.  
  1730.    The type of the argument is expected to be of aggregate type here.
  1731.  
  1732.    @@ What are the real semantics of this supposed to be??? */
  1733. int
  1734. build_default_unary_type_conversion (code, arg)
  1735.      enum tree_code code;
  1736.      tree *arg;
  1737. {
  1738.   tree type = TREE_TYPE (*arg);
  1739.   tree id = TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
  1740.     ? DECL_NAME (TYPE_NAME (type)) : TYPE_NAME (type);
  1741.   char *name = IDENTIFIER_POINTER (id);
  1742.  
  1743.   if (! TYPE_HAS_CONVERSION (type))
  1744.     {
  1745.       error ("type conversion required for type `%s'", name);
  1746.       return 0;
  1747.     }
  1748.  
  1749.   if (TYPE_HAS_INT_CONVERSION (type) && TYPE_HAS_REAL_CONVERSION (type))
  1750.     warning ("ambiguous type conversion for type `%s', defaulting to int", name);
  1751.   if (TYPE_HAS_INT_CONVERSION (type))
  1752.     *arg = build_type_conversion (code, integer_type_node, *arg, 1);
  1753.   else if (TYPE_HAS_REAL_CONVERSION (type))
  1754.     *arg = build_type_conversion (code, double_type_node, *arg, 1);
  1755.   else
  1756.     {
  1757.       *arg = build_type_conversion (code, ptr_type_node, *arg, 1);
  1758.       if (*arg == error_mark_node)
  1759.     error ("ambiguous pointer conversion");
  1760.     }
  1761.   if (*arg == 0)
  1762.     {
  1763.       error ("default type conversion for type `%s' failed", name);
  1764.       return 0;
  1765.     }
  1766.   return 1;
  1767. }
  1768.